/**
 * 
 */
package com.demo2do.fms.admin.entity.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.demo2do.fms.entity.authority.RoleTemplate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;

import com.demo2do.fms.entity.authority.User;

/**
 * @author Ysy
 */
public class Login implements UserDetails {

    private static final long serialVersionUID = -2977758312542405134L;

    /** 用户 ID */
    private Long id;
    
    /** 登录名  */
    private String name;
    
    private String password;
    
    /**  权限 */
    private String authority;
    
    /** 分区code：分区用户所属分区 */
    private String branch;
    
    /** 服务供应商：一级/二级供应商用户所属供应商， 对应Supplier  */
    private Long supplierId;
    
    /** 运输业务供应商：运输业务供应商用户所属供应商， 对应TransportationCompany */
    private Long transOrgId;
    
    /** 可操作运输主业务的数据权限 */
    private List<Long> orgIds;
    
    /** 是否是VAM供方用户 */
    private boolean vam;
    
    private Map<String, Set<String>> resources;
    
    /**
     * The default constructor
     */
    public Login() {
        
    }
         
    /**
     * 
     * @param user
     */
    public Login(User user) {
        this.id = user.getId();
        this.name = user.getLoginName();
        this.password = user.getPassword();
        this.authority = user.getAuthority();
        this.branch = user.getBranch();
        this.supplierId = user.getSupplierId();
        this.transOrgId = user.getOrgId();
        this.orgIds = new ArrayList<Long>();
        this.vam = user.isVam();
        this.resources = new HashMap<String, Set<String>>();
    }
    
    /**
     * Initialize orgIds
     * 
     * @param orgIds
     */
    public void initOrgIds(List<Long> orgIds) {
        this.orgIds = orgIds;
    }
    
    /**
     * Initialize resources
     * 
     * @param resources
     */
    public void initResources(Map<String, List<String>> resources) {
        
        for(Iterator<Map.Entry<String, List<String>>> iterator = resources.entrySet().iterator(); iterator.hasNext();) {
            Map.Entry<String, List<String>> entry = iterator.next();
            String key = entry.getKey();
            List<String> values = entry.getValue();
            this.addResource(key, values);
        }

    }
    
    /**
     * Add resource from key and value
     * 
     * @param key
     * @param values
     */
    public void addResource(String key, List<String> values) {
        if(this.resources != null && this.resources.containsKey(key)) {
            this.resources.get(key).addAll(values);
        } else {
            this.resources.put(key, new LinkedHashSet<String>(values));
        }
    }
    
    /**
     * Get resource from key
     * 
     * @param key
     * @return
     */
    public Set<String> getResource(String key) {
        return this.resources.get(key);
    }

    /**
     * 
     * @param type
     * @param key
     * @return
     */
    public boolean hasResource(String type, String key) {
        return this.resources.containsKey(type) && this.resources.get(type).contains(key);
    }
    
    /**
     * 
     * @param roles
     * @return
     */
    public boolean hasAnyPrincipalRole(String... roles) {
        
        Set<String> roleSet = AuthorityUtils.authorityListToSet(this.getAuthorities());

        for (String role : roles) {
            if (roleSet.contains(role)) {
                return true;
            }
        }
        
        return false;
    }
    
    /**
     * @return
     */
    public boolean isRegionUser() {
        return orgIds.size() > 1;
    }
    
    /**
     * @return
     */
    public Object getCurrOrgIds() {
        if (orgIds.size() > 1) {
            return orgIds;
        } else {
            return transOrgId;
        }
    }
    
    /**
     * 
     * @return
     */
    public boolean isSuper(){
        if(this.authority.contains("SUPER_USER")){
            return true;
        }
        return false;
    }
    
    /**
     * @return
     */
    public boolean isAdminUser() {
        if(this.authority.contains("ADMIN_USER")) {
            return true;
        }
        return false;
    }

    public boolean isCommonStorageSupplierUser(){
        return this.authority.contains(RoleTemplate.COMMON_STORAGE_USER.toString());
    }
    
    /**
     * @return
     */
    public boolean isBranchUser() {
        if(this.authority.contains("BRANCH_USER")) {
            return true;
        }
        return false;
    }
    
    /**
     * @return
     */
    public boolean isFmsUser() {
        if(this.authority.contains("FMS_USER")) {
            return true;
        }
        return false;
    }
    
    /**
     * @return
     */
    public boolean isSponsorUser() {
        if(this.authority.contains("SPONSOR_USER")) {
            return true;
        }
        return false;
    }
    
    /**
     * @return
     */
    public boolean isSupplierUser() {
        if(this.authority.contains("SUPPLIER_USER")) {
            return true;
        }
        return false;
    }
    
    /**
     * 
     * @return
     */
    public boolean isSupplier(){
        
        if(this.supplierId != null){
            return true;
        }
        
        return false;
        
    }
    
    /* (non-Javadoc)
     * @see org.springframework.security.core.userdetails.UserDetails#getAuthorities()
     */
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return AuthorityUtils.commaSeparatedStringToAuthorityList(this.authority);
    }

    /* (non-Javadoc)
     * @see org.springframework.security.core.userdetails.UserDetails#getPassword()
     */
    public String getPassword() {
        return this.password;
    }

    /* (non-Javadoc)
     * @see org.springframework.security.core.userdetails.UserDetails#getUsername()
     */
    public String getUsername() {
        return this.name;
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonExpired()
     */
    public boolean isAccountNonExpired() {
        return true;
    }

    /* (non-Javadoc)
     * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonLocked()
     */
    public boolean isAccountNonLocked() {
        return true;
    }

    /* (non-Javadoc)
     * @see org.springframework.security.core.userdetails.UserDetails#isCredentialsNonExpired()
     */
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.security.core.userdetails.UserDetails#isEnabled()
     */
    public boolean isEnabled() {
        return true;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the authority
     */
    public String getAuthority() {
        return authority;
    }

    /**
     * @return the branch
     */
    public String getBranch() {
        return branch;
    }

    /**
     * @return the supplierId
     */
    public Long getSupplierId() {
        return supplierId;
    }

    /**
     * @return the transOrgId
     */
    public Long getTransOrgId() {
        return this.transOrgId;
    }

    /**
     * @return the orgIds
     */
    public List<Long> getOrgIds() {
        return orgIds;
    }
    
    /**
     * @return
     */
    public boolean isVam() {
        return vam;
    }

    /**
     * @return the resources
     */
    public Map<String, Set<String>> getResources() {
        return resources;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @param authority the authority to set
     */
    public void setAuthority(String authority) {
        this.authority = authority;
    }

    /**
     * @param branch the branch to set
     */
    public void setBranch(String branch) {
        this.branch = branch;
    }

    /**
     * @param supplierId the supplierId to set
     */
    public void setSupplierId(Long supplierId) {
        this.supplierId = supplierId;
    }

    /**
     * @param transOrgId the transOrgId to set
     */
    public void setTransOrgId(Long transOrgId) {
        this.transOrgId = transOrgId;
    }

    /**
     * @param orgIds the orgIds to set
     */
    public void setOrgIds(List<Long> orgIds) {
        this.orgIds = orgIds;
    }
    
    /**
     * @param vam
     */
    public void setVam(boolean vam) {
        this.vam = vam;
    }

    /**
     * @param resources the resources to set
     */
    public void setResources(Map<String, Set<String>> resources) {
        this.resources = resources;
    }
}
